package workservice;

import java.lang.reflect.InvocationTargetException;
import java.util.ServiceLoader;

import eatapi.EatApi;
import transportapi.Transportation;
import workapi.Work;

public class WorkImpl implements Work {
    @Override
    public void work() throws Exception {
        System.out.println("开始工作了");
        //获取服务
        EatApi eatApi = ServiceLoader.load(EatApi.class).findFirst().get();
        //喝口水
        eatApi.drink();
        //反射获取 Transportation实现了类
        Transportation transportation = getTransportation();
        //出去一趟
        transportation.transport();
        //吃点东西
        eatApi.eat();
        //喝口水
        eatApi.drink();
    }

    private Transportation getTransportation() throws Exception {
        Class<Transportation> transportationClass = (Class<Transportation>) Class.forName("transportservice.TransportationImpl");
        Transportation transportation = transportationClass.getDeclaredConstructor().newInstance();
        return transportation;
    }
}
